'use strict';
const {getDefaultModelConfig} = require("../../utils/common");

module.exports = app => {
    const { STRING, INTEGER, BIGINT, TINYINT } = app.Sequelize;
    const Distribution = app.model.define(
        'Distribution',
        {
            id: {type: INTEGER, primaryKey: true, autoIncrement: true},
            rate: {
                type: TINYINT(3).UNSIGNED,
                allowNull: false,
                comment: '分销比例(1-100)'
            },
            description: {
                type: STRING(255),
                allowNull: true,
                comment: '描述'
            },
            user_type: {
                type: INTEGER,
                defaultValue: 1,
                comment: '1 普通用户 2 商家 3 一级分销商 4 二级分销商'
            },
            user_id: {
                type: BIGINT,
            },
            ...getDefaultModelConfig(app)
        },
        {
            tableName: 'distribution',
            timestamps: true,
            createdAt: 'create_time',
            updatedAt: 'update_time',
        }
    );
    Distribution.associate = function () {
        // 关联用户
        app.model.Business.Distribution.belongsTo(app.model.Business.User, {
            foreignKey: 'user_id',
            targetKey: 'id',
            as: 'user'
        });
    }
    return Distribution
};
